//
//  ClubGameDetailNewView.swift
//  77Club
//
//  Created by Apple on 2024/10/10.
//

import UIKit
import Cosmos

class ClubGameDetailPicCateView: UIView {

    private let bgView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.2)).corner(11).end()
    private let numLabel = c_make_ex(UILabel.self)
        .bgColor(.black.alpha(0.2)).corner(11)
        .alignment(.center).color(.white).font(10.font_medium).end()
    private let playButton = c_make_ex(UIButton.self)
        .bgImage(UIImage.colorToImage(color: .clear)).corner(9)
        .bgImage(UIImage.colorToImage(color: .white), state: .selected)
        .color("#2E3033",state: .selected).color(.white).font(10.font_medium)
        .text("真机试玩").end()
    private let avatarButton = c_make_ex(UIButton.self)
        .bgImage(UIImage.colorToImage(color: .clear)).corner(9)
        .bgImage(UIImage.colorToImage(color: .white), state: .selected)
        .color("#2E3033",state: .selected).color(.white).font(10.font_medium)
        .text("视频").end()
    private let imgButton = c_make_ex(UIButton.self)
        .bgImage(UIImage.colorToImage(color: .clear)).corner(9)
        .bgImage(UIImage.colorToImage(color: .white), state: .selected)
        .color("#2E3033",state: .selected).color(.white).font(10.font_medium)
        .text("图集").end()
    
    var updateCateData:((_:String)->())?
    
    var numStr: String = ""{
        didSet{
            numLabel.text = numStr
        }
    }
    
    var cateStr: String = ""{
        didSet{
            if cateStr == "1"{
                avatarButton.isSelected = true
                playButton.isSelected = false
                imgButton.isSelected = false
            }else if cateStr == "2"{
                playButton.isSelected = true
                avatarButton.isSelected = false
                imgButton.isSelected = false
            }else if cateStr == "3"{
                imgButton.isSelected = true
                avatarButton.isSelected = false
                playButton.isSelected = false
            }
        }
    }
    
    var cateArray: [String]?{
        didSet{
            guard let cateArr = cateArray else { return }            
            if cateArr.contains("1") {
                avatarButton.isHidden = false
            }else{
                avatarButton.isHidden = true
                avatarButton.widthConstraint?.constant = 0
            }
            if cateArr.contains("2") {
                playButton.isHidden = false
            }else{
                playButton.isHidden = true
                playButton.widthConstraint?.constant = 0
            }
            if cateArr.contains("3") {
                imgButton.isHidden = false
            }else{
                imgButton.isHidden = true
                imgButton.widthConstraint?.constant = 0
            }
            bgView.widthConstraint?.constant = CGFloat(60*cateArr.count) + CGFloat(4*(cateArr.count+1))
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        subviews{
            bgView.subviews{
                avatarButton
                playButton
                imgButton
            }
            numLabel
        }
        
        numLabel.trailing(24.0.Scale).bottom(8.0.Scale).width(40.0.Scale).height(22.0.Scale)
        bgView.width(188.0.Scale).height(22.0.Scale).bottom(8.0.Scale).centerHorizontally(offset: -25)
        avatarButton.leading(2).width(60.0.Scale).height(18.0.Scale).centerVertically()
        playButton.width(60.0.Scale).height(18.0.Scale).centerVertically()
        playButton.Leading == avatarButton.Trailing + 2
        imgButton.width(60.0.Scale).height(18.0.Scale).centerVertically()
        imgButton.Leading == playButton.Trailing + 2
        
        bgView.isUserInteractionEnabled = true
        avatarButton.addTarget(self, action: #selector(clickAvatarButton), for: .touchUpInside)
        playButton.addTarget(self, action: #selector(clickPlayButton), for: .touchUpInside)
        imgButton.addTarget(self, action: #selector(clickImgsButton), for: .touchUpInside)
        
        avatarButton.isHidden = true
        playButton.isHidden = true
        imgButton.isHidden = true
    }
    
    @objc private func clickAvatarButton(){
        if !avatarButton.isSelected {
            avatarButton.isSelected = true
            playButton.isSelected = false
            imgButton.isSelected = false
            updateCateData?("1")
        }
    }
    @objc private func clickPlayButton(){
        if !playButton.isSelected {
            avatarButton.isSelected = false
            playButton.isSelected = true
            imgButton.isSelected = false
            updateCateData?("2")
        }
    }
    @objc private func clickImgsButton(){
        if !imgButton.isSelected {
            avatarButton.isSelected = false
            playButton.isSelected = false
            imgButton.isSelected = true
            updateCateData?("3")
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

class ClubGameDetailScoreView: UIView {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.2)).corner(8).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color(.white).font(14.font_medium)
        .text("乐玩评分").alignment(.center).end()
    let scoreLabel = c_make_ex(UILabel.self)
        .color("#E05A00").font(24.font_wide)
        .alignment(.center).end()
    private let bottomLabel = c_make_ex(UILabel.self)
        .color(.white.alpha(0.6)).font(12.font_medium)
        .alignment(.center).end()
    private var fiveStarView = CosmosView()
    private var fourStarView = CosmosView()
    private var ThreeStarView = CosmosView()
    private var twoStarView = CosmosView()
    private var oneStarView = CosmosView()
    private var fiveSliderView = UISlider()
    private var fourSliderView = UISlider()
    private var ThreeSliderView = UISlider()
    private var twoSliderView = UISlider()
    private var oneSliderView = UISlider()
    private let fiveValueLabel = c_make_ex(UILabel.self)
        .color(.white.alpha(0.5)).font(9.font_medium)
        .alignment(.right).end()
    private let fourValueLabel = c_make_ex(UILabel.self)
        .color(.white.alpha(0.5)).font(9.font_medium)
        .alignment(.right).end()
    private let threeValueLabel = c_make_ex(UILabel.self)
        .color(.white.alpha(0.5)).font(9.font_medium)
        .alignment(.right).end()
    private let twoValueLabel = c_make_ex(UILabel.self)
        .color(.white.alpha(0.5)).font(9.font_medium)
        .alignment(.right).end()
    private let oneValueLabel = c_make_ex(UILabel.self)
        .color(.white.alpha(0.5)).font(9.font_medium)
        .alignment(.right).end()
    
    var model: ClubGameDetailInfoModel?{
        didSet{
            guard let point = model?.points else { return }

            let total = Double(point.total)
            for star in point.star_list {
                if star.star == 1 {
                    if star.num == 0 {
                        oneValueLabel.text = "0%"
                        oneSliderView.value = 0.0
                    }else{
                        let res = star.num/total * 100.0
                        oneValueLabel.text = "\(Int(res))%"
                        oneSliderView.value = Float(res)
                    }
                } else if star.star == 2 {
                    if star.num == 0 {
                        twoValueLabel.text = "0%"
                        twoSliderView.value = 0.0
                    }else{
                        let res = star.num/total * 100.0
                        twoValueLabel.text = "\(Int(res))%"
                        twoSliderView.value = Float(res)
                    }
                } else if star.star == 3 {
                    if star.num == 0 {
                        threeValueLabel.text = "0%"
                        ThreeSliderView.value = 0.0
                    }else{
                        let res = star.num/total * 100.0
                        threeValueLabel.text = "\(Int(res))%"
                        ThreeSliderView.value = Float(res)
                    }
                } else if star.star == 4 {
                    if star.num == 0 {
                        fourValueLabel.text = "0%"
                        fourSliderView.value = 0.0
                    }else{
                        let res = star.num/total * 100.0
                        fourValueLabel.text = "\(Int(res))%"
                        fourSliderView.value = Float(res)
                    }
                } else if star.star == 5 {
                    if star.num == 0 {
                        fiveValueLabel.text = "0%"
                        fiveSliderView.value = 0.0
                    }else{
                        let res = star.num/total * 100.0
                        fiveValueLabel.text = "\(Int(res))%"
                        fiveSliderView.value = Float(res)
                    }
                }
            }
            if total > 0 && total <= 9999 {
                self.bottomLabel.text = "\(point.total)评分"
            }else if total > 9999 {
                self.bottomLabel.text = "9999+评分"
            }else{
                self.bottomLabel.text = "评分"
            }            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        subviews{
            bgView.subviews{
                titleLabel
                scoreLabel
                bottomLabel
                fiveStarView
                fourStarView
                ThreeStarView
                twoStarView
                oneStarView
                fiveSliderView
                fourSliderView
                ThreeSliderView
                twoSliderView
                oneSliderView
                fiveValueLabel
                fourValueLabel
                threeValueLabel
                twoValueLabel
                oneValueLabel
            }
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(0).height(93.0.Scale)
        titleLabel.leading(12.0.Scale).top(8.0.Scale).height(20.0.Scale).width(61.0.Scale)
        scoreLabel.leading(12.0.Scale).top(36.0.Scale).height(30.0.Scale).width(61.0.Scale)
        bottomLabel.leading(12.0.Scale).bottom(8.0.Scale).height(17.0.Scale).width(61.0.Scale)
        fiveStarView.leading(78.0.Scale).top(36.0.Scale).height(8.0.Scale).width(40.0.Scale)
        fourStarView.leading(86.0.Scale).height(8.0.Scale).width(32.0.Scale)
        fourStarView.Top == fiveStarView.Bottom + 2.0.Scale
        ThreeStarView.leading(94.0.Scale).height(8.0.Scale).width(24.0.Scale)
        ThreeStarView.Top == fourStarView.Bottom + 2.0.Scale
        twoStarView.leading(102.0.Scale).height(8.0.Scale).width(16.0.Scale)
        twoStarView.Top == ThreeStarView.Bottom + 2.0.Scale
        oneStarView.leading(110.0.Scale).height(8.0.Scale).width(8.0.Scale)
        oneStarView.Top == twoStarView.Bottom + 2.0.Scale
        fiveSliderView.leading(122.0.Scale).height(4.0.Scale).trailing(40.0.Scale).top(35.0.Scale)
        fourSliderView.leading(122.0.Scale).height(4.0.Scale).trailing(40.0.Scale)
        fourSliderView.Top == fiveSliderView.Bottom + 6.0.Scale
        ThreeSliderView.leading(122.0.Scale).height(4.0.Scale).trailing(40.0.Scale)
        ThreeSliderView.Top == fourSliderView.Bottom + 6.0.Scale
        twoSliderView.leading(122.0.Scale).height(4.0.Scale).trailing(40.0.Scale)
        twoSliderView.Top == ThreeSliderView.Bottom + 6.0.Scale
        oneSliderView.leading(122.0.Scale).height(4.0.Scale).trailing(40.0.Scale)
        oneSliderView.Top == twoSliderView.Bottom + 6.0.Scale
        fiveValueLabel.trailing(12.0.Scale)
        fiveValueLabel.CenterY == fiveSliderView.CenterY
        fourValueLabel.trailing(12.0.Scale)
        fourValueLabel.CenterY == fourSliderView.CenterY
        threeValueLabel.trailing(12.0.Scale)
        threeValueLabel.CenterY == ThreeSliderView.CenterY
        twoValueLabel.trailing(12.0.Scale)
        twoValueLabel.CenterY == twoSliderView.CenterY
        oneValueLabel.trailing(12.0.Scale)
        oneValueLabel.CenterY == oneSliderView.CenterY
                
        setupStarView(starView: fiveStarView, score: 5.0)
        setupStarView(starView: fourStarView, score: 4.0)
        setupStarView(starView: ThreeStarView, score: 3.0)
        setupStarView(starView: twoStarView, score: 2.0)
        setupStarView(starView: oneStarView, score: 1.0)
        
        setupSliderView(makeView: fiveSliderView, value: 0)
        setupSliderView(makeView: fourSliderView, value: 0)
        setupSliderView(makeView: ThreeSliderView, value: 0)
        setupSliderView(makeView: twoSliderView, value: 0)
        setupSliderView(makeView: oneSliderView, value: 0)
    }
    
    private func setupStarView(starView: CosmosView, score: Double){
        
        starView.rating = score
        starView.settings.starSize = 6.0.Scale
        starView.settings.starMargin = 2.0.Scale
        starView.settings.fillMode = .full
        starView.settings.filledImage = "Common_fillStar".toImage
        starView.settings.emptyImage = UIImage()
        
        starView.isUserInteractionEnabled = false
    }
    
    private func setupSliderView(makeView: UISlider, value: Float){
        //  最小值
        makeView.minimumValue = 0.0
        //  最大值
        makeView.maximumValue = 100.0
        //  设置默认值
        //  滑动条有值部分颜色
        makeView.minimumTrackTintColor = "#FFC862".toColor
        //  滑动条没有值部分颜色
        makeView.maximumTrackTintColor = "#C8C9CC".toColor
        //  修改控制器图片
        makeView.setThumbImage(UIImage(), for: .normal)
        makeView.isUserInteractionEnabled = false
        makeView.value = value
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class ClubGameDetailPriceCell: UITableViewCell,UITableViewDelegate, UITableViewDataSource {
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .black.alpha(0.2)
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubGameDetailPriceItemCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 38.0.Scale
        tableView.layer.cornerRadius = 8.0.Scale
        tableView.layer.masksToBounds = true
        return tableView
    }()
    
    var models: [ClubGameDetailScrapysItemModel]?{
        didSet{
            guard let arr = models else { return }
            
            tableView.heightConstraint?.constant = Double(arr.count) * 38.0.Scale
            tableView.reloadData()
        }
    }

    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            tableView
        }
        
        tableView.leading(12.0.Scale).trailing(12.0.Scale).top(0).bottom(10.0.Scale).height(38.0.Scale)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return models?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubGameDetailPriceItemCell", for: indexPath) as! ClubGameDetailPriceItemCell
        cell.model = models?[indexPath.row]
        return cell
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ClubGameDetailPriceItemCell: UITableViewCell {
    
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.white).font(14.font_semiBold).end()
    private let priceLabel = c_make_ex(UILabel.self)
        .color(.white).font(14.font_medium)
        .alignment(.right).end()

    var model: ClubGameDetailScrapysItemModel?{
        didSet{
            guard let md = model else { return }
            nameLabel.text = md.source_text
            priceLabel.text = md.price
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            nameLabel
            priceLabel
        }
        
        nameLabel.leading(8.0.Scale).centerVertically()
        priceLabel.trailing(8.0.Scale).centerVertically()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
